<?php

class Controller_Vote extends Controller_Website
{

    public function before() {
        parent::before();
        $this->mod = Model::factory('directory');
        $this->man = Model::factory('manager');
        $this->section = 'index';
    }

	public function actionIndex()
	{
            $this->autoRender = FALSE;
            if(Request::$isAjax)
            {
                $response = array();
                if(!empty($_SESSION['identity']))
                {
                    $pictureId = (is_numeric($_POST['id'])) ? trim($_POST['id']) : '';
                    $userId = $_SESSION['identity']['id'];
                    $result =$this->manager->addVote($userId,$pictureId);
                    if($result){
                        $response['success'] = 'Yout vote has been added.Thank you!';
                        $response['result'] = $result;
                    }
                    else
                    {
                        $response['error'] = 'You have already voted for this item';
                    }

                }
                else
                {
                    $response['error']['redirect'] = 'You are not logged in.Only members can vote';

                }


                $this->response->setHeader('Content-Type', 'application/json');
                $this->response->body = json_encode($response);
            }

	}

}
